var mybudget = {
	router : {
		loadView : function(container, controller, viewname, callback) {
			if (typeof (localStorage['VIEW' + viewname]) == 'undefined') {
				_.data.request(controller, viewname, {}, function(rs) {
					localStorage.setItem('VIEW' + viewname, rs);
					var containerDom = null;
					if (typeof (_(container).length) == 'undefined') {
						containerDom = _(container);
					} else {
						containerDom = _(container)[0];
					}
					containerDom.innerHTML = localStorage['VIEW' + viewname];
					if (typeof (callback) == 'function') {
						setTimeout(function() {
							callback();
						}, 10);
					}
				});
			} else {
				_(container)[0].innerHTML = localStorage['VIEW' + viewname];
				if (typeof (callback) == 'function') {
					setTimeout(function() {
						callback();
					}, 10);
				}
			}
		},
		addTransaction : function() {
			this.loadView(_mainContainer, 'transaction', 'AddTransaction',
					function() {
						// Set datetime value
						// var dat = new Date();
						// document.getElementById('txtCreatedDate').value =
						// dat.toJSON().slice(0,10);
					});
		},
		updateTransaction : function(id) {
			this
					.loadView(
							_mainContainer,
							'transaction',
							'UpdateTransaction',
							function() {
								currententry = mybudget.Function.Transaction
										.FindEntryByID(id);
								localStorage.setItem('currententry',
										mybudget.utilities
												.domToString(currententry));
								// Set Data
								document.getElementById('txtID').value = currententry
										.getElementsByTagName('id')[0].textContent;
								document.getElementById('txtDescription').value = currententry
										.getElementsByTagName('description')[0].textContent;
								document.getElementById('btnDelete').setAttribute('entryid',currententry
										.getElementsByTagName('id')[0].textContent);
								//entryid
								// document.getElementById('txtCreatedDate').value
								// =
								// currententry.getElementsByTagName('createdDate')[0].textContent;
								var currentAmount = currententry
										.getElementsByTagName('amount')[0].textContent;
								if (Number(currentAmount) >= 0) {
									document.getElementById('txtAmount').value = currentAmount;
									document.getElementsByName('chkType')[1].checked = true;
								} else {
									document.getElementById('txtAmount').value = (-Number(currentAmount))
											+ '';
									document.getElementsByName('chkType')[0].checked = true;
								}
								// Set datetime value
								// var dat = new Date();
								// dat.setDate(currententry.getElementsByTagName('createdDate')[0].textContent.slice(0,2));
								// dat.setMonth(Number(currententry.getElementsByTagName('createdDate')[0].textContent.slice(3,5))+1);
								// dat.setYear(currententry.getElementsByTagName('createdDate')[0].textContent.slice(6,10));
								// document.getElementById('txtCreatedDate').value
								// = dat.toJSON().slice(0,10);
							});
		},
		commonCache : function() {
			this.loadView('#commonCache', 'mybudget', 'common', function() {
				console.log('OK');
			});
		},
		todayTransaction : function() {
			var date = new Date();
			localStorage.setItem('currentDate', date);
			localStorage.setItem('currentViewMode', 'date');
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByDate(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Date('
															+ mybudget.utilities
																	.GetFormattedDateString(date)
															+ ')';
												});
							});
		},
		previousdayTransaction : function() {
			var date = new Date(localStorage.getItem('currentDate'));
			date.setDate(date.getDate() - 1);
			localStorage.setItem('currentDate', date);
			localStorage.setItem('currentViewMode', 'date');
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByDate(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Date('
															+ mybudget.utilities
																	.GetFormattedDateString(date)
															+ ')';
												});
							});
		},
		nextdayTransaction : function() {
			var date = new Date(localStorage.getItem('currentDate'));
			date.setDate(date.getDate() + 1);
			var currentDate = new Date();
			if (date > currentDate) {
				return;
			}
			localStorage.setItem('currentViewMode', 'date');
			localStorage.setItem('currentDate', date);
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByDate(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Date('
															+ mybudget.utilities
																	.GetFormattedDateString(date)
															+ ')';
												});
							});
		},
		thisMonthTransaction : function() {
			var date = new Date();
			localStorage.setItem('currentDate', date);
			localStorage.setItem('currentViewMode', 'month');
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByMonth(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Month('
															+ mybudget.utilities
																	.GetFormattedDateMonthString(date)
															+ ')';
												});
							});
		},
		previousMonthTransaction : function() {
			var date = new Date(localStorage.getItem('currentDate'));
			date.setMonth(date.getMonth() - 1);
			localStorage.setItem('currentDate', date);
			localStorage.setItem('currentViewMode', 'month');
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByMonth(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Month('
															+ mybudget.utilities
																	.GetFormattedDateMonthString(date)
															+ ')';
												});
							});
		},
		nextMonthTransaction : function() {
			var date = new Date(localStorage.getItem('currentDate'));
			date.setMonth(date.getMonth() + 1);
			var currentDate = new Date();
			if (date > currentDate) {
				return;
			}
			localStorage.setItem('currentDate', date);
			localStorage.setItem('currentViewMode', 'month');
			mybudget.Function.Common
					.GetXSL(
							'transactionlist',
							function(xsl) {
								mybudget.Function.Transaction
										.GetEntriesByMonth(
												date,
												function(xml) {
													localStorage
															.setItem(
																	'currentEntriesSource',
																	mybudget.Function.Common.DecodeURI(xml));
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(xml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Month('
															+ mybudget.utilities
																	.GetFormattedDateMonthString(date)
															+ ')';
												});
							});
		},
		next : function() {
			if (localStorage.currentViewMode == 'date') {
				this.nextdayTransaction();
			} else {
				this.nextMonthTransaction();
			}
		},
		previous : function() {
			if (localStorage.currentViewMode == 'date') {
				this.previousdayTransaction();
			} else {
				this.previousMonthTransaction();
			}
		},
		sortByPayment : function() {
			mybudget.Function.Common.GetXSL('transactionlistorderbypayment',
					function(xsl) {
						var xml = localStorage.getItem('currentEntriesSource');
						_('.bodyMain')[0].innerHTML = mybudget.utilities
								.XMLTransform(mybudget.Function.Common.DecodeURI(xml), xsl);
					});
		},
		sortByRecieve : function() {
			mybudget.Function.Common.GetXSL('transactionlistorderbyrecieve',
					function(xsl) {
						var xml = localStorage.getItem('currentEntriesSource');
						_('.bodyMain')[0].innerHTML = mybudget.utilities
								.XMLTransform(mybudget.Function.Common.DecodeURI(xml), xsl);
					});
		},
		filterByPayment : function() {
			var filterByRec = function(node, callback) {
				if (node.nodeName.toLowerCase() != 'entries') {
					filterByRec(node.children[0], callback);
				} else {
					var filteredNode = [];
					for ( var i = 0; i < node.children.length; i++) {
						var currentNode = node.children[i];
						var amount = Number(currentNode
								.getElementsByTagName('amount')[0].textContent);
						if (amount < 0) {
							filteredNode.push(currentNode);
						}
					}
					var container = '<entries>';
					for ( var i = 0; i < filteredNode.length; i++) {
						container += mybudget.utilities
								.domToString(filteredNode[i]);
					}
					container += '</entries>';
					callback(container);
				}
			};
			mybudget.Function.Common.GetXSL('transactionlist', function(xsl) {
				var xml = localStorage.getItem('currentEntriesSource');
				var xmlDom = mybudget.utilities.xmlStringToDom(xml);
				filterByRec(xmlDom, function(finalxml) {
					_('.bodyMain')[0].innerHTML = mybudget.utilities
							.XMLTransform(mybudget.Function.Common.DecodeURI(finalxml), xsl);
				});

			});
		},
		filterByRecieve : function() {
			var filterByRec = function(node, callback) {
				if (node.nodeName.toLowerCase() != 'entries') {
					filterByRec(node.children[0], callback);
				} else {
					var filteredNode = [];
					for ( var i = 0; i < node.children.length; i++) {
						var currentNode = node.children[i];
						var amount = Number(currentNode
								.getElementsByTagName('amount')[0].textContent);
						if (amount > 0) {
							filteredNode.push(currentNode);
						}
					}
					var container = '<entries>';
					for ( var i = 0; i < filteredNode.length; i++) {
						container += mybudget.utilities
								.domToString(filteredNode[i]);
					}
					container += '</entries>';
					callback(container);
				}
			};
			mybudget.Function.Common.GetXSL('transactionlist', function(xsl) {
				var xml = localStorage.getItem('currentEntriesSource');
				var xmlDom = mybudget.utilities.xmlStringToDom(xml);
				filterByRec(xmlDom, function(finalxml) {
					_('.bodyMain')[0].innerHTML = mybudget.utilities
							.XMLTransform(mybudget.Function.Common.DecodeURI(finalxml), xsl);
				});

			});
		},
		init : function(){
			$$('body').swipeLeft(function() {
				if (((new Date()).getTime() - mybudget.data.lastestSwipe.getTime()) > 1000){
					mybudget.router.next();
					mybudget.data.lastestSwipe = new Date();
				}
			});
			$$('body').swipeRight(function() {
				if (((new Date()).getTime() - mybudget.data.lastestSwipe.getTime()) > 1000){
				    mybudget.router.previous();
				    mybudget.data.lastestSwipe = new Date();
				}
			});
		}
		

	},
	data:{
		lastestSwipe : new Date()
	}
};

// ==================================================
// Action
mybudget.action = {};

mybudget.action.DeleteEntry = function(obj) {
	var key = obj.attributes['entryid'].value;
	var confirmed = window.confirm("System is going to delete this entry");
	if (confirmed) {
		mybudget.Function.Transaction.DeleteEntryByKey(key, function(rs) {
			if (rs) {
				alert("Delete success!");
			} else {
				alert("System busy now. Please try later!");
			}
		});
	}
};
mybudget.action.UpdateEntry = function(obj) {
	var key = obj.attributes['entryid'].value;
	var confirmed = true;
	if (confirmed) {
		mybudget.router.updateTransaction(key);
	}
};

mybudget.utilities = {
	requireFields : [],
	serializeFormData : function(form, rootName) {
		if (typeof (rootName) == 'undefined') {
			rootName = "data";
		}
		var xmlDom = document.createElement(rootName);
		var requireFields = [];
		for (i = 0; i < form.elements.length; i++) {
			var element = form.elements[i];
			if (element.name !== "") {
				if (this.isRequire(element) && element.value == "") {
					requireFields.push(element.name);
				}
				if (element.type == 'radio') {
					if (element.checked) {
						var node = document.createElement(element.name
								.toLowerCase());
						node.textContent = encodeURIComponent(element.value);
						xmlDom.appendChild(node);
					}
				} else {
					var node = document.createElement(element.name
							.toLowerCase());
					node.textContent = encodeURIComponent(element.value);
					xmlDom.appendChild(node);
				}

			}
		}
		if (requireFields.length > 0) {
			mybudget.utilities.requireFields = requireFields;
			return null;
		}
		return xmlDom;
	},
	isRequire : function(element) {
		if (element.hasAttribute("required")) {
			return true;
		}
		return false;
	},
	domToString : function(node) {
		if (typeof (XMLSerializer) !== 'undefined') {
			var serializer = new XMLSerializer();
			return serializer.serializeToString(node);
		} else if (node.xml) {
			return node.xml;
		}

	},
	encodeUTF8 : function(str) {
		return unescape(encodeURIComponent(str));
	},
	decodeUTF8 : function(str) {
		return decodeURIComponent(escape(str));
	},
	xmlStringToDom : function(xml) {
		var xmlDoc = null;
		if (window.DOMParser) {
			parser = new DOMParser();
			xmlDoc = parser.parseFromString(xml, "text/xml");
		} else // Internet Explorer
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			xmlDoc.loadXML(xml);
		}
		return xmlDoc;
	},
	domSynchronize : function(source, target) {
		try {
			// This method is only work when source and target is root and 1
			// level of child
			if (source.children.length == 1) {
				source = source.children[0];
			}
			if (target.children.length == 1) {
				target = target.children[0];
			}
			for (i = 0; i < target.children.length; i++) {
				var currentTargeNode = target.children[i].nodeName
						.toLowerCase();
				if (currentTargeNode != '') {
					for (j = 0; j < source.children.length; j++) {
						var currentSourceNode = source.children[j].nodeName
								.toLowerCase();
						if (currentSourceNode == currentTargeNode) {
							target.children[i].textContent = source.children[j].textContent;
						}
					}
				}
			}
		} catch (ex) {
			console.log(ex);
		}
	},
	GetParameter : function(key, value) {
		return '<' + key + '>' + value + '</' + key + '>';
	},
	GetFormattedDateString : function(date) {
		return (date.getMonth() + 1) + "/" + date.getDate() + "/"
				+ date.getFullYear();
	},
	GetFormattedDateMonthString : function(date) {
		return (date.getMonth() + 1) + "/" + date.getFullYear();
	},
	XMLTransform : function(xmlStr, xslStr) {
		var xml = mybudget.utilities.xmlStringToDom(xmlStr);
		var xsl = mybudget.utilities.xmlStringToDom(xslStr);
		if (xml == null || xsl == null) {
			console.log(xmlStr);
			console.log(xslStr);
		}
		var output = '';
		// code for IE
		if (window.ActiveXObject) {
			output = xml.transformNode(xsl);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation
				&& document.implementation.createDocument) {
			var xsltProcessor = new XSLTProcessor();
			xsltProcessor.importStylesheet(xsl);
			var resultDocument = xsltProcessor.transformToFragment(xml,
					document);
			output = mybudget.utilities.domToString(resultDocument);
		}
		return output;
	}
};

mybudget.Function = {};
mybudget.Function.Transaction = {};
// budget function
mybudget.Function.Transaction.AddEntry = function() {
	var tsf = document.getElementById('addtransactionform');
	var processAndValidate = function(node) {
		if (node == null) {
			return;
		}
		if (node.nodeName.toLowerCase().trim() == 'chktype') {
			var type = node.textContent.trim();
			for ( var i = 0; i < node.parentNode.children.length; i++) {
				if (node.parentNode.children[i].nodeName.toLowerCase().trim() == 'amount') {
					var amountValue = Math
							.abs(Number(node.parentNode.children[i].textContent
									.trim()));
					// payment type

					if (type == 1) {
						amountValue = -amountValue;
					}
					node.parentNode.children[i].textContent = amountValue
							.toString();
				}
			}
		}
		if (node.children.length > 0) {
			for ( var n = 0; n < node.children.length; n++) {
				processAndValidate(node.children[n]);
			}
		}
	};
	var dataDom = mybudget.utilities.serializeFormData(tsf, 'Entry');
	if (dataDom == null) {
		alert('Follow field is required: '
				+ mybudget.utilities.requireFields.join(', '));
	} else {
		// Process data
		processAndValidate(dataDom);
		// Create standard dom
		var TransactionDom = mybudget.Model.Transaction();
		mybudget.utilities.domSynchronize(dataDom, TransactionDom);

		var strDom = mybudget.utilities.domToString(TransactionDom);
		console.log(strDom);
		_.data.request('transaction', 'AddTransactionSubmit', strDom, function(
				rs) {
			if (rs == null) {
				rs = 'null';
			}
			try {
				rs = rs.trim();
			} catch (e) {
			}
			console.log(rs);
			if (rs == 'true') {
				mybudget.Function.Transaction.CancleAddEntry();
			} else {
				if (mybudget.Function.Transaction.Validate()) {
					alert('Can not create entry now. Please try later');
				}
			}
		}, function(ex) {
			alert('Can not create entry now. Please try later');
			console.log(ex);
		});
	}

};

mybudget.Function.Transaction.Validate = function() {
	if (document.getElementById('txtDescription')) {
		if (document.getElementById('txtDescription').value.length < 5
				|| document.getElementById('txtDescription').value.length > 100) {
			alert('Description must be  > 4 charaters and < 100 charaters');
			return false;
		}
	}
	if (document.getElementById('txtAmount')) {
		if (Math.abs(Number(document.getElementById('txtAmount').value)) > 100000000) {
			alert('Amount can not be greater that ABS(100,000,000)');
			return false;
		}
	}
};

mybudget.Function.Transaction.UpdateEntry = function() {
	var tsf = document.getElementById('updatetransactionform');
	var processAndValidate = function(node) {
		if (node == null) {
			return;
		}
		if (node.nodeName.toLowerCase() == 'chktype') {
			var type = node.textContent;
			for ( var i = 0; i < node.parentNode.children.length; i++) {
				if (node.parentNode.children[i].nodeName.toLowerCase() == 'amount') {
					var amountValue = Math
							.abs(Number(node.parentNode.children[i].textContent));
					// payment type
					if (type == 1) {
						amountValue = -amountValue;
					}
					node.parentNode.children[i].textContent = amountValue
							.toString();
				}
			}
		}
		if (node.children.length > 0) {
			for ( var n = 0; n < node.children.length; n++) {
				processAndValidate(node.children[n]);
			}
		}
	};
	var dataDom = mybudget.utilities.serializeFormData(tsf, 'Transaction');
	if (dataDom == null) {
		alert('Follow field is required: '
				+ mybudget.utilities.requireFields.join(', '));
	} else {
		// Process data
		processAndValidate(dataDom);
		// Create standard dom
		var TransactionDom = mybudget.Model.Transaction();
		mybudget.utilities.domSynchronize(dataDom, TransactionDom);

		var strDom = mybudget.utilities.domToString(TransactionDom);
		console.log(strDom);
		_.data
				.request(
						'transaction',
						'UpdateTransactionSubmit',
						strDom,
						function(rs) {
							if (rs == null) {
								rs = 'null';
							}
							try {
								rs = rs.trim();
							} catch (e) {
							}
							console.log(rs);
							if (rs == 'true') {
								mybudget.Function.Transaction.CancleAddEntry();
							} else {
								if (mybudget.Function.Transaction.Validate()) {
									alert('Can not create entry now. Please try later');
								}
							}
						},
						function(ex) {
							alert('Can not update entry now. Please try later');
							console.log(ex);
						});
	}

};

mybudget.Function.Transaction.CancleAddEntry = function() {
	console.log('-------start---------');
	if (localStorage.currentViewMode == 'date') {
		var date = new Date(localStorage.getItem('currentDate'));
		console.log('-------date---------');
		console.log(date);
		console.log('-------date---------');
		mybudget.Function.Common
				.GetXSL(
						'transactionlist',
						function(xsl) {
							mybudget.Function.Transaction
									.GetEntriesByDate(
											date,
											function(newxml) {
												localStorage.setItem(
														'currentEntriesSource',
														mybudget.Function.Common.DecodeURI(newxml));
												for ( var i = 0; i < 2; i++) {
													_('.bodyMain')[0].innerHTML = mybudget.utilities
															.XMLTransform(
																	mybudget.Function.Common.DecodeURI(newxml),
																	xsl);
													_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Date('
															+ mybudget.utilities
																	.GetFormattedDateString(date)
															+ ')';
												}
											});
						});
	} else {
		var date = new Date(localStorage.getItem('currentDate'));
		mybudget.Function.Common
				.GetXSL(
						'transactionlist',
						function(xsl) {
							mybudget.Function.Transaction
									.GetEntriesByMonth(
											date,
											function(xml) {
												localStorage.setItem(
														'currentEntriesSource',
														mybudget.Function.Common.DecodeURI(xml));
												_('.bodyMain')[0].innerHTML = mybudget.utilities
														.XMLTransform(
																mybudget.Function.Common.DecodeURI(xml),
																xsl);
												_('.entryOperator_Center_Info_Content')[0].innerHTML = 'Month('
														+ mybudget.utilities
																.GetFormattedDateMonthString(date)
														+ ')';
											});
						});
	}

};

mybudget.Function.Transaction.GetEntriesByDate = function(date, callback) {
	var data = mybudget.utilities.GetParameter('viewDate', mybudget.utilities
			.GetFormattedDateString(date));
	_.data.request('transaction', 'GetTransactionByDate', data, function(rs) {
		callback(rs);
	}, function(ex) {
		callback(ex);
	});
};

mybudget.Function.Transaction.GetEntriesByMonth = function(date, callback) {
	var data = mybudget.utilities.GetParameter('viewDate', mybudget.utilities
			.GetFormattedDateString(date));
	_.data.request('transaction', 'GetTransactionByMonth', data, function(rs) {
		callback(rs);
	}, function(ex) {
		callback(ex);
	});
};

mybudget.Function.Transaction.DeleteEntryByKey = function(key, callback) {
	var data = mybudget.utilities.GetParameter('key', key);
	_.data.request('transaction', 'DeleteTransactionByKey', data, function(rs) {
		callback(rs);
	}, function(ex) {
		callback(ex);
	});
};

mybudget.Function.Transaction.FindEntryByID = function(id) {
	var findEntryByID = function(node, id) {
		var entries = node.getElementsByTagName('entry');
		for ( var i = 0; i < entries.length; i++) {
			var ids = entries[i].getElementsByTagName('id');
			if (ids[0].textContent == id) {
				return entries[i];
			}
		}
	};
	var currentDom = mybudget.utilities
			.xmlStringToDom(localStorage.currentEntriesSource);
	return findEntryByID(currentDom, id);
};

mybudget.Function.DeleteAllCookies = function() {
	var cookies = document.cookie.split(";");

	for ( var i = 0; i < cookies.length; i++) {
		var cookie = cookies[i];
		var eqPos = cookie.indexOf("=");
		var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
		document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
	}
};
// ====================================================================================
// Common
mybudget.Function.Common = {};
mybudget.Function.Common.GetXSL = function(name, callback) {
	if (typeof (localStorage['XSLCache' + name]) != 'undefined') {
		callback(localStorage['XSLCache' + name]);
		return;
	} else {
		var data = mybudget.utilities.GetParameter('fileName', name);
		_.data.request('mybudget', 'loadxslfile', data, function(rs) {
			localStorage.setItem('XSLCache' + name, rs);
			callback(localStorage['XSLCache' + name]);
		}, function(ex) {
			callback(ex);
		});
	}
};
mybudget.Function.Common.DecodeURI = function(input){
	try{
		var output = decodeURI(input);
		return output;
	}catch(ex){
		try{
			var output = decodeURIComponent(input);
			return output;
		}catch(e){
			return input;
		}
	}
};
// ---------------------------------------------------------------------------
// Model
mybudget.Model = {};
mybudget.Model.Transaction = function() {
	var date = new Date();
	var template = '<entry>';
	template += '       <id>0</id>';
	template += '       <description>' + '...' + '</description>';
	template += '       <amount>' + 100000 + '</amount>';
	template += '       <createdDate>' + date.toLocaleDateString()
			+ '</createdDate>';
	template += '       <account>1</account>';
	template += '   </entry>';
	return mybudget.utilities.xmlStringToDom(template);
};
